package cc.linker.steplink.crm.merchant.controller.wecom;

import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.merchant.service.wecom.WecomService;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/wecom")
public class WecomController {

    @Autowired
    private UserInfoUtil userInfoUtil;

    @Autowired
    private WecomService wecomService;

    /**
     * 客户列表
     *
     * @param : keyword  关键字
     */
    @PostMapping("/custome/list")
    public PageInfo customeList(@RequestBody Map<String, Object> map, HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        map.put("orgId", orgId);
        Integer pageIndex = 1;
        Integer pageSize = 10;
        if(!StringUtils.isEmpty(map.get("pageSize")) ){
            pageSize = Integer.valueOf(map.get("pageSize").toString());
        }
        if(!StringUtils.isEmpty(map.get("pageIndex")) ){
            pageIndex = Integer.valueOf(map.get("pageIndex").toString());
        }
        PageHelper.startPage(pageIndex, pageSize);
        return new PageInfo<>(wecomService.customeList(map));
    }

    /**
     * 客户详情
     *
     * @param : customerId  关键字
     */
    @PostMapping("/custome/info")
    public Object customeInfo(@RequestBody Map<String, Object> map, HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        map.put("orgId", orgId);
        return wecomService.customeInfo(map);
    }

    /**
     * 获取token
     *
     * @param : customerId  关键字
     */
    @GetMapping("/getToken")
    @ResponseBody
    public Object getToken(HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        return wecomService.getTokenByOrgId(orgId);
    }

}
